package com.yima.baseproj.module.sys.clazz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yima.baseproj.module.sys.user.SysUserService;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ClassService extends ServiceImpl<ClassMapper, ClassEntity> {

    @Autowired
    private SysUserService userService;

    /**
     * 分页+模糊查询
     * @param page
     * @param searchModel
     * @return
     */
    public Page<Map> findAllByPage(Page page, ClassEntity searchModel) {
        //获取user 对应的kgId
        int kgId = userService.findKgIdByUserId();
        if(kgId==0){
            return baseMapper.findBySearchVo(page,searchModel);
        }else {
            return baseMapper.findByKgId(kgId,page,searchModel);
        }

    }

    /**
     * 新增操作
     * @param classEntity
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(ClassEntity classEntity) {
        baseMapper.insert(classEntity);
    }

    public ClassEntity findById(int id){
        return  baseMapper.selectById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    public void edit(ClassEntity classEntity){
        baseMapper.updateById(classEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByIds(int id) {
        baseMapper.deleteById(id);
    }


    /**
     * 得到班级列表
     * @param classEntity
     */
    public List<Map> findAllByList(ClassEntity classEntity) {
        List<Map> classList = baseMapper.findBySearchVo(classEntity);
        return classList;
    }

    public List<ClassEntity> findAllByKgId(int kgId) {
        List<ClassEntity> classList = baseMapper.selectList(new QueryWrapper<ClassEntity>().eq("kg_id",kgId));
        return classList;
    }

    public ClassEntity findByName(String className){
        return baseMapper.selectOne(new QueryWrapper<ClassEntity>()
                                    .like("class_name",className) ) ;
    }

}
